package com.atguigu.yygh.order.service.impl;

import com.atguigu.yygh.common.config.YyghException;
import com.atguigu.yygh.enums.PaymentStatusEnum;
import com.atguigu.yygh.enums.PaymentTypeEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.atguigu.yygh.order.mapper.PaymentMapper;
import com.atguigu.yygh.order.service.OrderService;
import com.atguigu.yygh.order.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @Author zhmwi
 * @Date 2021/7/23
 * @Dersciption
 */
@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, PaymentInfo> implements PaymentService {

    @Autowired
    private OrderService orderService;
    @Override
    public void savePaymentInfo(OrderInfo orderInfo, Integer paymentType) {
        //保证订单的一致性: 一个订单一个交易记录
        //根据order_id + 支付类型 查询paymentInfo是否已存在

        QueryWrapper<PaymentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",orderInfo.getId());
        queryWrapper.eq("payment_type",paymentType);
        Integer count = baseMapper.selectCount(queryWrapper);
        if (count > 0){
            return;
        }

        //创建新的交易记录
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(paymentType);
        //对外业务编号
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        //支付状态:支付中
        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());
        String subject = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd")+"|"+orderInfo.getHosname()+"|"+orderInfo.getDepname()+"|"+orderInfo.getTitle();
        //交易内容
        paymentInfo.setSubject(subject);
        //支付金额
        paymentInfo.setTotalAmount(orderInfo.getAmount());
        baseMapper.insert(paymentInfo);
    }

    //更改订单状态 更改交易状态
    @Override
    public void paySuccess(String out_trade_no, Integer payment_type, Map<String, String> resultMap) {
        //根据交易号 + 支付类型查询支付信息
        QueryWrapper<PaymentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("out_trade_no",out_trade_no);
        queryWrapper.eq("payment_type",payment_type);
        PaymentInfo paymentInfo = baseMapper.selectOne(queryWrapper);
        if (paymentInfo == null){
            throw new YyghException();
        }
        //判断如果交易状态不是支付中则返回(微信回调可能设置已支付)
        if (paymentInfo.getPaymentStatus() != PaymentStatusEnum.UNPAID.getStatus()){
            return ;
        }
        //修改支付状态
        PaymentInfo paymentInfoUpd = new PaymentInfo();
        paymentInfoUpd.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());
        paymentInfoUpd.setTradeNo(resultMap.get("transaction_id"));
        paymentInfoUpd.setCallbackTime(new Date());
        paymentInfoUpd.setCallbackContent(resultMap.toString());
        baseMapper.update(paymentInfoUpd,
                new QueryWrapper<PaymentInfo>().eq("out_trade_no",out_trade_no));


        //根据交易号查询订单
        OrderInfo orderInfo =
                orderService.getOne(
                        new QueryWrapper<OrderInfo>().eq("out_trade_no",out_trade_no));
        if (orderInfo == null){
            throw new YyghException();
        }
        orderInfo.setOrderStatus(PaymentStatusEnum.PAID.getStatus());
        orderService.updateById(orderInfo);
        //TODO 调用医院接口 更新支付状态
    }

    //查询交易记录
    @Override
    public PaymentInfo getPaymentInfo(Long orderId, Integer paymentType) {
        QueryWrapper queryWrapper = new QueryWrapper<PaymentInfo>();
        queryWrapper.eq("order_id",orderId);
        queryWrapper.eq("payment_type",paymentType);
        PaymentInfo paymentInfo = baseMapper.selectOne(queryWrapper);
        return paymentInfo;
    }
}
